import { isObject } from "../shared/index";
import { mutableHandlers, readonlyHandlers, shallowReadonlyHandlers } from "./baseHandler";

// 公共枚举
export const enum ReactiveFlags {
    IS_REACTIVE = '__v_isReactive',
    IS_READONLY = '__v_isReadonly',
}

// 创建普通响应式对象
export function reactive(raw) {
    return createActiveObject(raw, mutableHandlers)
}

// 创建readonly响应式对象
export function readonly(raw) {
    return createActiveObject(raw, readonlyHandlers)
}

// 只有响应式对象（reactive）才能被代理，否则一律返回false
export function isReactive(value) {
    return !!value[ReactiveFlags.IS_REACTIVE]
}

// 是否代理对象
export function isProxy(value) {
    return isReactive(value) || isReadonly(value);
}

// 只有响应式对象（reactive）才能被代理，否则一律返回false
export function isReadonly(value) {
    return !!value[ReactiveFlags.IS_READONLY]
}

// 浅层式只读对象
export function shallowReadonly(raw) {
    return createActiveObject(raw, shallowReadonlyHandlers)
}

// 创建响应式对象
function createActiveObject(raw: any, baseHandlers) {
    if (!isObject(raw)) {
        console.warn('raw only accepts object!');
    }
    return new Proxy(raw, baseHandlers);
}